package com.hava.contentsale.web.controller.api;

import com.hava.contentsale.meta.UserVO;
import com.hava.contentsale.service.ProductService;
import com.hava.contentsale.utils.web.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by yanfa on 2016/11/20.
 * 异步数据接口 - 2.删除产品
 *
 * 详细见文档
 * https://my.oschina.net/hava/blog/779440
 */
@Controller
@RequestMapping(value = "/api")
public class DeleteApiController {

    @Autowired
    ProductService productServiceImpl;

    @ResponseBody
    @RequestMapping(value = "/delete",method= RequestMethod.POST,produces = MediaType.JSON)
    public JsonResult api_elete(HttpSession httpSession,
                              @RequestParam("id") Long product_id)
    {
        System.out.println("Class DeleteApiController Method api_elete");
        System.out.println("[product_id]:" + product_id);

        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        JsonResult jsonResult = new JsonResult();
        if(product_id == null)
        {
            //请求参数有错误 http status code is 400
            jsonResult.setCode("400");
            jsonResult.setMessage("购买失败");
            jsonResult.setResult(false);
        }
        else
        {
            if(userVO == null)
            {
                //401 Unauthorized当前请求需要验证
                jsonResult.setCode("401");
                jsonResult.setMessage("购买失败");
                jsonResult.setResult(false);
            }
            else if(userVO.getUsertype() == 1)
            {
                //删除产品，如果正确删除，则返回200，并表示删除成功
                try {
                    this.productServiceImpl.delete(product_id);

                    jsonResult.setCode("200");
                    jsonResult.setMessage("购买成功");
                    jsonResult.setResult(true);
                }
                catch (Exception e)
                {
                    System.out.println("[Exception]:" + e.toString());
                    jsonResult.setCode("400");
                    jsonResult.setMessage("购买失败");
                    jsonResult.setResult(false);
                }
            }
            else if(userVO.getUsertype() == 1)
            {
                //403 Forbidden，当卖家点击进入购买时，则发生没有权限，导致操作拒绝
                jsonResult.setCode("403");
                jsonResult.setMessage("购买失败");
                jsonResult.setResult(false);
            }
            else
            {
                //403 Forbidden未知身份发生购买，拒绝
                jsonResult.setCode("403");
                jsonResult.setMessage("购买失败");
                jsonResult.setResult(false);
            }
        }

        return jsonResult;
    }
}
